package com.lcz.authority.admin.controller.monitor;

import com.lcz.authority.admin.service.SysOperLogService;
import com.lcz.authority.common.annotation.ApiResource;
import com.lcz.authority.common.annotation.Log;
import com.lcz.authority.common.core.page.PageResult;
import com.lcz.authority.common.domain.R;
import com.lcz.authority.common.domain.entity.SysOperLogEntity;
import com.lcz.authority.common.enums.AppCodeEnum;
import com.lcz.authority.common.enums.ResBizTypeEnum;
import com.lcz.authority.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lcz
 * @desc 操作日志记录
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */
@RestController
@RequestMapping("/monitor/operlog")
@Log(openLog = false)
@ApiResource(name = "操作日志管理", resBizType = ResBizTypeEnum.SYSTEM, appCode = AppCodeEnum.SYSTEMMODULES)
@Api(tags = "操作日志管理")
public class SysOperlogController {

    @Autowired
    private SysOperLogService operLogService;

    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping(value = "/list", name = "操作日志-分页")
    @ApiOperation("分页")
    public R list(SysOperLogEntity operLog) {
        PageResult<SysOperLogEntity> page = operLogService.selectOperLogPage(operLog);
        return R.ok().put(page);
    }


    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping(value = "/{operIds}", name = "操作日志-删除")
    @ApiOperation("删除")
    public R remove(@PathVariable String[] operIds) {
        return R.ok(operLogService.deleteOperLogByIds(operIds));
    }

    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping(value = "/clean", name = "操作日志-清空")
    @ApiOperation("清空")
    public R clean() {
        operLogService.cleanOperLog();
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping(value = "/export", name = "操作日志-导出")
    @ApiOperation("导出操作日志数据")
    public void export(HttpServletResponse response, SysOperLogEntity sysOperLogEntity) {
        List<SysOperLogEntity> list = operLogService.selectOperLogPage(sysOperLogEntity).getRows();
        ExcelUtil<SysOperLogEntity> util = new ExcelUtil<>(SysOperLogEntity.class);
        util.exportExcel(response, list, "操作日志");
    }
}
